package com.base.web.core.config.shiro;

import com.base.web.auth.service.UserService;
import com.base.web.core.common.BaseConstant;
import com.base.web.model.master.auth.User;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description: 自定义权限信息的验证
 *
 * @author : RENTIE
 *         Date: 2018-07-04 14:16
 */
public class CustomRealm extends AuthorizingRealm {

    @Autowired
    private UserService userService;

    /**
     * 授权
     *
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //从主体传的认证信息中获取用户登录名
        String loginName = (String) principalCollection.getPrimaryPrincipal();

        //从数据库或缓存中获得角色数据
        Set<String> roles = getRolesByLoginName(loginName);

        //从数据库或缓存中获得权限数据
        Set<String> permissions = getPermissionsByloginName(loginName);

        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        authorizationInfo.setStringPermissions(permissions);
        authorizationInfo.setRoles(roles);

        return authorizationInfo;
    }

    private Set<String> getPermissionsByloginName(String loginName) {
        List<String> permissions = userService.queryPermissionsByLoginName(loginName);
        Set<String> sets = new HashSet<String>(permissions);
        return sets;
    }

    /**
     * 数据库获取角色集合
     *
     * @param loginName
     * @return
     */
    private Set<String> getRolesByLoginName(String loginName) {
        List<String> roles = userService.queryRolesByLoginName(loginName);
        Set<String> sets = new HashSet<String>(roles);

        return sets;
    }

    /**
     * 认证
     *
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //1、从主体传的认证信息中获取用户名
        String loginName = (String) authenticationToken.getPrincipal();

        //2、通过用户名获取数据库中的凭证
        String password = getPassWordByloginName(loginName);
        if (password == null) {
            return null;
        }

        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(loginName, password, "customRealm");

        //加盐
        authenticationInfo.setCredentialsSalt(ByteSource.Util.bytes(BaseConstant.MD5_SALT));

        return authenticationInfo;
    }

    /**
     * 数据库查询凭证
     *
     * @param loginName
     * @return
     */
    private String getPassWordByloginName(String loginName) {
        User user = new User();
        user.setLoginName(loginName);
        user = userService.selectOne(user);
        if (user != null) {
            return user.getPassword();
        } else {
            return null;
        }

    }

}
